package com.gxnzd.communityelderly.commom;

import com.gxnzd.communityelderly.entity.Diagnosis;
import com.gxnzd.communityelderly.entity.Reservation;
import com.gxnzd.communityelderly.mapper.DiagnosisMapper;
import com.gxnzd.communityelderly.mapper.ReservationMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;


/**
 * @Author 姚源卿
 * @date 2024-12-08 16:59:42
 * @description 监听redis过期事件
 */
@Slf4j
public class KeyExpiredListener extends KeyExpirationEventMessageListener {


    @Resource
    private DiagnosisMapper diagnosisMapper;

    @Resource
    private ReservationMapper reservationMapper;

    @Resource
    private RedisTemplate redisTemplate;


    public KeyExpiredListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    public void onMessage(Message message, byte[] pattern) {
        String expireKey = message.toString();
        //将过期的记录状态改为过期
        Long diagnosisId = Long.parseLong(expireKey);
        Diagnosis diagnosis = diagnosisMapper.selectById(diagnosisId);
        Reservation reservation = reservationMapper.selectById(diagnosis.getReservationId());
        //如果是待支付才能修改
        if(reservation.getStatus().equals("4")){
            reservation.setStatus("5");
        }
        reservationMapper.updateById(reservation);
    }
}